package com.common.display
{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	/**
	 *位图九宫格实现 
	 * @author lake
	 * var bitmapScale9Grid :BitmapScale9Grid = new BitmapScale9Grid(bitmap,34,42,18,42);
			this.addChild(bitmapScale9Grid);
			bitmapScale9Grid.x = 50;
			bitmapScale9Grid.y = 100;
			bitmapScale9Grid.width = 100;
			bitmapScale9Grid.height = 100;
	 */	
	public class BitmapScale9Grid extends Sprite
	{
		/**
		 *源图片 
		 */		
		private var source : Bitmap;
		/**
		 *顶距离 
		 */		
		private var scaleGridTop 		: Number;
		/**
		 *底距离 
		 */		
		private var scaleGridBottom 	: Number;
		/**
		 *左距离 
		 */		
		private var scaleGridLeft 		: Number;
		/**
		 *右距离 
		 */		
		private var scaleGridRight 	: Number;
		/**
		 *左上切片 
		 */		
		private var leftUp 		: Bitmap;
		/**
		 * 左中切片
		 */		
		private var leftCenter 	: Bitmap;
		/**
		 *左底切片 
		 */		
		private var leftBottom 	: Bitmap;
		/**
		 *中上切片 
		 */		
		private var centerUp 		: Bitmap;
		/**
		 *中切片 
		 */		
		private var center 		: Bitmap;
		/**
		 *中下切片 
		 */		
		private var centerBottom 	: Bitmap;
		/**
		 *右上切片 
		 */		
		private var rightUp 		: Bitmap;
		/**
		 *右中切片 
		 */		
		private var rightCenter 	: Bitmap;
		/**
		 *右下切片 
		 */		
		private var rightBottom 	: Bitmap;
		/**
		 *宽高 
		 */		
		private var _width : Number;
		private var _height : Number;
		/**
		 *最小宽高 
		 */		
		private var minWidth : Number;
		private var minHeight : Number;
		/**
		 * 构造 初始化
		 * 左上角开始(0,0)
		 * @param source 源图片
		 * @param scaleGridTop 上
		 * @param scaleGridBottom 下
		 * @param scaleGridLeft 左
		 * @param scaleGridRight 右
		 * 
		 */		
		public function BitmapScale9Grid(source:Bitmap, 
										 scaleGridTop:Number, 
										 scaleGridBottom:Number, 
										 scaleGridLeft:Number, 
										 scaleGridRight:Number ) 
		{
			this.source = source;
			this.scaleGridTop = scaleGridTop;
			this.scaleGridBottom = scaleGridBottom;
			this.scaleGridLeft = scaleGridLeft;
			this.scaleGridRight = scaleGridRight;
			init();
			
		}
		
		private function init() : void 
		{
			_width = source.width;
			_height = source.height;
			
			leftUp = getBitmap(0, 0, scaleGridLeft, scaleGridTop);
			this.addChild(leftUp);
			
			leftCenter = getBitmap(0, scaleGridTop, scaleGridLeft, scaleGridBottom - scaleGridTop);
			this.addChild(leftCenter);
			
			leftBottom = getBitmap(0, scaleGridBottom, scaleGridLeft, source.height - scaleGridBottom);
			this.addChild(leftBottom);
			
			centerUp = getBitmap(scaleGridLeft, 0, scaleGridRight - scaleGridLeft, scaleGridTop);
			this.addChild(centerUp);
			
			center = getBitmap(scaleGridLeft, scaleGridTop, scaleGridRight - scaleGridLeft, scaleGridBottom - scaleGridTop);
			this.addChild(center);
			
			centerBottom = getBitmap(scaleGridLeft, scaleGridBottom, scaleGridRight - scaleGridLeft, source.height - scaleGridBottom);
			this.addChild(centerBottom);
			
			rightUp = getBitmap(scaleGridRight, 0, source.width - scaleGridRight, scaleGridTop);
			this.addChild(rightUp);
			
			rightCenter = getBitmap(scaleGridRight, scaleGridTop, source.width - scaleGridRight, scaleGridBottom - scaleGridTop);
			this.addChild(rightCenter);
			
			rightBottom = getBitmap(scaleGridRight, scaleGridBottom, source.width - scaleGridRight, source.height - scaleGridBottom);
			this.addChild(rightBottom);
			
			minWidth = leftUp.width + rightBottom.width;
			minHeight = leftBottom.height + rightBottom.height;
		}
		
		private function getBitmap(x:Number, y:Number, w:Number, h:Number) : Bitmap 
		{
			var bit:BitmapData = new BitmapData(w, h);
			bit.copyPixels(source.bitmapData, new Rectangle(x, y, w, h), new Point(0, 0));
			var bitMap:Bitmap = new Bitmap(bit);
			bitMap.x = x;
			bitMap.y = y;
			return bitMap;
		}
		
		override public function set width(w : Number) : void
		{
			if(w < minWidth)
			{
				w = minWidth;
			}
			_width = w;
			refurbishSize();
		}
		
		override public function set height(h : Number) : void 
		{
			if(h < minHeight) 
			{
				h = minHeight;
			}
			_height = h;
			refurbishSize();
		}
		
		private function refurbishSize() : void 
		{
			leftCenter.height = _height - leftUp.height - leftBottom.height;
			leftBottom.y = _height - leftBottom.height;
			centerUp.width = _width - leftUp.width - rightUp.width;
			center.width = centerUp.width;
			center.height = leftCenter.height;
			centerBottom.width = center.width;
			centerBottom.y = leftBottom.y;
			rightUp.x = _width - rightUp.width;
			rightCenter.x = rightUp.x;
			rightCenter.height = center.height;
			rightBottom.x = rightUp.x;
			rightBottom.y = leftBottom.y;
		}
	}
}